package com.example.wan_android.adapter

import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.wan_android.BR
import com.example.wan_android.R
import com.example.wan_android.core.CoreViewHolder
import com.example.wan_android.databinding.AdapterAuthorTabBinding
import com.example.wan_android.http.bean.AuthorNameItemData
import com.example.wan_android.viewmodel.AuthorViewModel

class AuthorTabAdapter(vm: AuthorViewModel) :
    CoreAdapter<AdapterAuthorTabBinding, AuthorViewModel, AuthorNameItemData>(
        BR.data,
        BR.viewModel,
        vm
    ) {
    override fun getItemLayoutId(viewType: Int): Int {
        return R.layout.adapter_author_tab
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CoreViewHolder {
        val holder = super.onCreateViewHolder(parent, viewType)
        holder.binding.root.findViewById<TextView>(R.id.tv_author_tab)
            .setOnClickListener {
                val position = holder.adapterPosition
                mViewModel?.setClear(true)
                mViewModel?.setCurrentIndex(0)
                mViewModel?.setCurrentAuthor(mListData[position].name)
                mViewModel?.setPosition(position)
                mViewModel?.getAuthorArticleInfoData()
                notifyDataSetChanged()
                (holder.binding.root.parent as? RecyclerView)?.smoothScrollToPosition(mViewModel?.position?.value!!)
            }
        return holder
    }

    override fun onBindViewHolder(holder: CoreViewHolder, position: Int) {
        super.onBindViewHolder(holder, position)
        holder.binding.root.findViewById<TextView>(R.id.tv_author_tab).isSelected =
            mViewModel?.position?.value == position
    }
}